package com.frxuexi.sign.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.lib_base.http.RequestType
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.room.SignEntity
import com.frxuexi.lib_base.utils.DateTimeUtils
import com.frxuexi.sign.R
import com.frxuexi.sign.SignIntent
import com.frxuexi.sign.SignViewModel
import com.frxuexi.sign.databinding.ActivitySignRoomBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/sign/SignRoomActivity")
@AndroidEntryPoint
class SignRoomActivity : BaseActivity<ActivitySignRoomBinding,SignIntent,SignViewModel>() {
    override val binding: ActivitySignRoomBinding by lazy { ActivitySignRoomBinding.inflate(layoutInflater) }
    override val vm: SignViewModel by viewModels<SignViewModel>()

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.ADD->{
                                binding.signStartTime.setText("已打卡")
                                val signEntity = it.data as Long
                                SPUtils.getInstance().put("rid",signEntity)
//                                SPUtils.getInstance().put("rid",signEntity.morning)

                            }
                            RequestType.UPDATE->{
                                binding.signEndTime.setText("已打卡")
                            }
                            else->{
                                val entity = it.data as SignEntity
                                if(entity.morning.isNotEmpty()){
                                    binding.signStartTime.setText("已打卡")
                                }
                                if(entity.after.isNotEmpty()){
                                    binding.signEndTime.setText("已打卡")
                                }
                            }

                        }

                    }


                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        val date = DateTimeUtils.getDate(System.currentTimeMillis())
        vm.sendIntent(SignIntent.selectSigByDate(date))

        binding.btnSign.setOnClickListener {
            val time = DateTimeUtils.getTime(System.currentTimeMillis())
            if(binding.signStartTime.text.toString()!="已打卡"){
                val entity =
                    SignEntity(null, date, DateTimeUtils.getTime(System.currentTimeMillis()), "")
                SPUtils.getInstance().put("rtime",time)
                vm.sendIntent(SignIntent.insertSign(entity))
            }else{
                if(binding.signEndTime.text.toString()!="已打卡"){
                    val entity =
                        SignEntity(SPUtils.getInstance().getLong("rid"), date, SPUtils.getInstance().getString("rtime"), DateTimeUtils.getTime(System.currentTimeMillis()))
                    vm.sendIntent(SignIntent.updateSign(entity))
                }
            }
        }
    }

}